package org.xiaom.butler.activity;
//download by http://www.codefans.net
import org.json.JSONException;
import org.json.JSONObject;
import org.xiaom.butler.R;
import org.xiaom.butler.bean.Hotel;
import org.xiaom.butler.util.AppHelper;
import org.xiaom.butler.util.NetHelper;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WeatherActivity extends Activity {
	private Hotel hotel;
	//记录壁纸的文件
	public static final String WALLPAPER_FILE="wallpaper_file";
	//缓存天气的文件
	public static final String STORE_WEATHER="store_weather";
	//当前Activity的根布局
	private LinearLayout rootLayout;
	private Handler handler =new Handler(){
		public void handleMessage(android.os.Message msg) {
	 	    setWeatherSituation( msg.obj.toString());
		};
	};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(this, getString(R.string.app_weather_tip), Toast.LENGTH_LONG).show();
        hotel=(Hotel) getIntent().getExtras().get("hotel");
        //设置窗口特征,为不显示标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.weather);
        
        rootLayout = (LinearLayout)findViewById(R.id.rootLayout); 
        
    	//设置默认的壁纸
    	SharedPreferences.Editor editor = getSharedPreferences(WALLPAPER_FILE, MODE_PRIVATE).edit();
    	editor.putInt("wellpaper", R.drawable.app_bg02);
    	editor.commit();
        //得到保存的城市天气
        final SharedPreferences sp = getSharedPreferences(AppHelper.SHARED_PREFERENCES_NAME ,MODE_PRIVATE);
    	final String cityCode= hotel.getCityNumber().toString();
    	final SharedPreferences shared = getSharedPreferences(STORE_WEATHER, MODE_PRIVATE); 
    	new Thread(){
    		public void run() {
    			String info = "http://m.weather.com.cn/data/"+cityCode+".html";
      	      	info=new NetHelper().getString(info);
    			Message msg=new Message();
    			msg.obj=info;
    			handler.sendMessage(msg);
    		};
    	}.start();
    }
    public void setWeatherSituation(String jsonStr) {
      try {
    	    //==========================解析JSON得到天气===========================
			JSONObject json=new JSONObject(jsonStr).getJSONObject("weatherinfo");
			TextView tempText = null;
			ImageView imageView=null;
			int weather_icon = 0;
			
			//建立一个缓存天气的文件
			SharedPreferences.Editor editor = getSharedPreferences(STORE_WEATHER, MODE_PRIVATE).edit();
			
			//得到城市
			jsonStr=json.getString("city");
			tempText=(TextView)findViewById(R.id.cityField);
			tempText.setText(jsonStr);
			editor.putString("city", jsonStr);
			
			//得到阳历日期
			jsonStr= json.getString("date_y") ;
			jsonStr= jsonStr+"("+json.getString("week")+")";
			tempText=(TextView)findViewById(R.id.date_y);
			tempText.setText(jsonStr);
			editor.putString("date_y", jsonStr);
			//得到农历
			jsonStr= json.getString("date");
			tempText=(TextView)findViewById(R.id.date);
			tempText.setText(jsonStr);
			editor.putString("date", jsonStr);
			//得到温度
			jsonStr= json.getString("temp1");
			tempText=(TextView)findViewById(R.id.currentTemp);
			tempText.setText(jsonStr);
			editor.putString("temp1", jsonStr);
			//得到天气
			jsonStr= json.getString("weather1");
			tempText=(TextView)findViewById(R.id.currentWeather);
			tempText.setText(jsonStr);
			editor.putString("weather1", jsonStr);
			//天气图标
			jsonStr= json.getString("img_title1");
			imageView=(ImageView)findViewById(R.id.weather_icon01);
			weather_icon = getWeatherBitMapResource(jsonStr);
			imageView.setImageResource(weather_icon);
			editor.putInt("img_title1", weather_icon);
			//得到风向
			jsonStr= json.getString("wind1");
			tempText=(TextView)findViewById(R.id.currentWind);
			tempText.setText(jsonStr);
			editor.putString("wind1", jsonStr);
			//得到建议
			jsonStr= json.getString("index_d");
			tempText=(TextView)findViewById(R.id.index_d);
			tempText.setText(jsonStr);
			editor.putString("index_d", jsonStr);
			
			//得到明天的天气
			jsonStr= json.getString("weather2");
			tempText=(TextView)findViewById(R.id.weather02);
			tempText.setText(jsonStr);
			editor.putString("weather2", jsonStr);
			//明天的图标
			jsonStr= json.getString("img_title2");
			imageView=(ImageView)findViewById(R.id.weather_icon02);
			weather_icon = getWeatherBitMapResource(jsonStr);
			imageView.setImageResource(weather_icon);
			editor.putInt("img_title2", weather_icon);
			//明天的气温
			jsonStr= json.getString("temp2");
			tempText=(TextView)findViewById(R.id.temp02);
			tempText.setText(jsonStr);
			editor.putString("temp2", jsonStr);
			//明天的风力
			jsonStr= json.getString("wind2");
			tempText=(TextView)findViewById(R.id.wind02);
			tempText.setText(jsonStr);
			editor.putString("wind2", jsonStr);
			
			//后天的天气
			jsonStr= json.getString("weather3");
			tempText=(TextView)findViewById(R.id.weather03);
			tempText.setText(jsonStr);
			editor.putString("weather3", jsonStr);
			//后天天气图标
			jsonStr= json.getString("img_title3");
			imageView=(ImageView)findViewById(R.id.weather_icon03);
			weather_icon = getWeatherBitMapResource(jsonStr);
			imageView.setImageResource(weather_icon);
			editor.putInt("img_title3", weather_icon);
			//后天的气温
			jsonStr= json.getString("temp3");
			tempText=(TextView)findViewById(R.id.temp03);
			tempText.setText(jsonStr);
			editor.putString("temp3", jsonStr);
			//后天的风力
			jsonStr= json.getString("wind3");
			tempText=(TextView)findViewById(R.id.wind03);
			tempText.setText(jsonStr);
			editor.putString("wind3", jsonStr);
			
			//设置一个有效日期为5小时
			long validTime = System.currentTimeMillis();
			validTime = validTime + 5*60*60*1000;
			editor.putLong("validTime", validTime);
			
			//保存
		//	editor.commit();
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
    }
    
    //根据已定的缓存文件来得到天气情况
    public void setWeatherSituation(SharedPreferences shared) {
    	String info = null;
    	TextView tempText = null;
		ImageView imageView=null;
    	
    	//得到城市
		info = shared.getString("city", "");
		tempText=(TextView)findViewById(R.id.cityField);
		tempText.setText(info);
		
		//得到阳历日期
		info= shared.getString("date_y", "");
		tempText=(TextView)findViewById(R.id.date_y);
		tempText.setText(info);
		//得到农历
		info= shared.getString("date", "");
		tempText=(TextView)findViewById(R.id.date);
		tempText.setText(info);
		//得到温度
		info= shared.getString("temp1", "");
		tempText=(TextView)findViewById(R.id.currentTemp);
		tempText.setText(info);
		//得到天气
		info= shared.getString("weather1", "");
		tempText=(TextView)findViewById(R.id.currentWeather);
		tempText.setText(info);
		//天气图标
		imageView=(ImageView)findViewById(R.id.weather_icon01);
		imageView.setImageResource(shared.getInt("img_title1", 0));
		//得到风向
		info= shared.getString("wind1", "");
		tempText=(TextView)findViewById(R.id.currentWind);
		tempText.setText(info);
		//得到建议
		info= shared.getString("index_d", "");
		tempText=(TextView)findViewById(R.id.index_d);
		tempText.setText(info);
		
		//得到明天的天气
		info= shared.getString("weather2", "");
		tempText=(TextView)findViewById(R.id.weather02);
		tempText.setText(info);
		//明天的图标
		imageView=(ImageView)findViewById(R.id.weather_icon02);
		imageView.setImageResource(shared.getInt("img_title2", 0));
		//明天的气温
		info= shared.getString("temp2", "");
		tempText=(TextView)findViewById(R.id.temp02);
		tempText.setText(info);
		//明天的风力
		info= shared.getString("wind2", "");
		tempText=(TextView)findViewById(R.id.wind02);
		
		//后天的天气
		info= shared.getString("weather3", "");
		tempText=(TextView)findViewById(R.id.weather03);
		tempText.setText(info);
		//后天天气图标
		imageView=(ImageView)findViewById(R.id.weather_icon03);
		imageView.setImageResource(shared.getInt("img_title3", 0));
		//后天的气温
		info= shared.getString("temp3", "");
		tempText=(TextView)findViewById(R.id.temp03);
		tempText.setText(info);
		//后天的风力
		info= shared.getString("wind3", "");
		tempText=(TextView)findViewById(R.id.wind03);
		tempText.setText(info);
    }
    
    //由天气情况得到图片
    public static int getWeatherBitMapResource(String weather) {
    	Log.i("weather_info", "============="+weather+"===============");
    	if(weather.equals("晴")) {
    		return R.drawable.weathericon_condition_01;
    	} else if(weather.equals("多云")) {
    		return R.drawable.weathericon_condition_02;
    	} else if(weather.equals("阴")) {
    		return R.drawable.weathericon_condition_04;
    	} else if(weather.equals("雾")) {
    		return R.drawable.weathericon_condition_05;
    	} else if(weather.equals("沙尘暴")) {
    		return R.drawable.weathericon_condition_06;
    	} else if(weather.equals("阵雨")) {
    		return R.drawable.weathericon_condition_07;
    	} else if(weather.equals("小雨")||weather.equals("小到中雨")) {
    		return R.drawable.weathericon_condition_08;
    	} else if(weather.equals("大雨")) {
    		return R.drawable.weathericon_condition_09;
    	} else if(weather.equals("雷阵雨")) {
    		return R.drawable.weathericon_condition_10;
    	} else if(weather.equals("小雪")) {
    		return R.drawable.weathericon_condition_11;
    	} else if(weather.equals("大雪")) {
    		return R.drawable.weathericon_condition_12;
    	} else if(weather.equals("雨夹雪")) {
    		return R.drawable.weathericon_condition_13;
    	} else {
    		return R.drawable.weathericon_condition_17;
    	}
    }
}